//
//  ApiConfig.swift
//  MT
//
//  Created by zhang qiang on 2018/11/21.
//  Copyright © 2018年 张强. All rights reserved.

import Foundation

/// 向api的参数中加入验证签名信息
///
/// - para : para
/// - Returns: new para
public func AddSignInfoToApi(para: [String : Any]) -> [String : Any]{
    
    var newPara : [String: Any] = [:]
    
    func getSignString(_ item : Any) -> String{
        
        if JSONSerialization.isValidJSONObject(item) {
            do{
                let data = try JSONSerialization.data(withJSONObject: item, options: .prettyPrinted)
                return String.init(data: data, encoding: .utf8)!
            }catch{
                return ""
            }
        }
        return "\(item)"
    }
    
    for (key, value) in para{
        newPara[key] = value
    }
    
    newPara["timestamp"] = String(Int(Date().timeIntervalSince1970))
    newPara["os"] = "ios"
    //newPara["version"] = CurrentServerVersion
    
    return newPara
}


public class ApiConfig{
    
    public static let  config = ApiConfig()
   
    private init(){
        token = UserDefaults.standard.string(forKey: HttpRequestTokenKey)
    }
    
    /// BaseUrl
    public private(set) var ServerBaseUrl  = "http://xxxxx/v1/"
    

    /// token
     public internal(set) var token : String? {
        didSet{
            UserDefaults.standard.set(token, forKey: HttpRequestTokenKey)
            UserDefaults.standard.synchronize()
        }
    }
    
    
    /// token 存储的key
    private let HttpRequestTokenKey = "HttpRequestTokenKey"
}
